package xpn.platform.common.util;

import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class Tool {

	public static final String PWD_PATTERN = "^(?![A-Za-z0-9]+$)(?![a-z0-9\\W]+$)(?![A-Za-z\\W]+$)(?![A-Z0-9\\W]+$)[a-zA-Z0-9\\W]{8,}$";


	public static boolean validPwd(String pwd){
		boolean valid=false;
		if(pwd!=null){
			if(pwd.matches(PWD_PATTERN)){
				valid=true;
			}
		}
      return valid;
	}

	public static boolean isEmpty(Object obj) {
		if (obj == null) {
			return true;
		}
		if ((obj instanceof List)) {
			return ((List<?>) obj).size() == 0;
		}
		if ((obj instanceof String)) {
			return "".equals(((String) obj).trim());
		}
		return false;
	}

	public static boolean isNumber(Object obj) {
		return isInteger(obj) || isLong(obj) || isFloat(obj) || isDouble(obj);
	}

	public static boolean isInteger(Object obj) {
		if (obj == null) {
			return false;
		}
		if (obj instanceof String) {
			try {
				Integer.parseInt((String) obj);
				return true;
			} catch (Exception e) {
				return false;
			}
		}
		if ((obj instanceof Integer)) {
			return true;
		}
		return false;
	}

	public static boolean isLong(Object obj) {
		if (obj == null) {
			return false;
		}
		if ((obj instanceof String)) {
			try {
				Long.parseLong((String) obj);
				return true;
			} catch (Exception e) {
				return false;
			}
		}
		if ((obj instanceof Long)) {
			return true;
		}
		return false;
	}

	public static boolean isFloat(Object obj) {
		if (obj == null) {
			return false;
		}
		if ((obj instanceof String)) {
			try {
				Float.parseFloat((String) obj);
				return true;
			} catch (Exception e) {
				return false;
			}
		}
		if ((obj instanceof Float)) {
			return true;
		}
		return false;
	}

	public static boolean isDouble(Object obj) {
		if (obj == null) {
			return false;
		}
		if ((obj instanceof String)) {
			try {
				Double.parseDouble((String) obj);
				return true;
			} catch (Exception e) {
				return false;
			}
		}
		if ((obj instanceof Double)) {
			return true;
		}
		return false;
	}

	public static Integer getWeekOfYear(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.setFirstDayOfWeek(Calendar.MONDAY);
		return calendar.get(Calendar.WEEK_OF_YEAR);
	}

	public static Integer getWeekOfYear() {
		Calendar calendar = Calendar.getInstance();
		calendar.setFirstDayOfWeek(Calendar.MONDAY);
		return calendar.get(Calendar.WEEK_OF_YEAR);
	}

	public static Integer getYear(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.YEAR);
	}

	public static Integer getYear() {
		Calendar calendar = Calendar.getInstance();
		return calendar.get(Calendar.YEAR);
	}

	public static Integer getMonth(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.MONTH) + 1;
	}

	public static Integer getMonth() {
		Calendar calendar = Calendar.getInstance();
		return calendar.get(Calendar.MONTH) + 1;
	}

	public static Integer getDay(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.DAY_OF_MONTH);
	}

	public static Integer getDay() {
		Calendar calendar = Calendar.getInstance();
		return calendar.get(Calendar.DAY_OF_MONTH);
	}

	public static Integer getHourOfDay(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.HOUR_OF_DAY);
	}

	public static Integer getHourOfDay() {
		Calendar calendar = Calendar.getInstance();
		return calendar.get(Calendar.HOUR_OF_DAY);
	}

	public static Integer getMinute(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.MINUTE);
	}

	public static Integer getMinute() {
		Calendar calendar = Calendar.getInstance();
		return calendar.get(Calendar.MINUTE);
	}

	public static String getDateString(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
		return format.format(calendar.getTime());
	}

	// md5加密字符串
	public static final String MD5(final String s) {
		//小写MD5
		final char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };

		if (s.length() == 32) { // 郑波2017-11-25增加，如果密码已经md5加密过，则不加密
			return s;
		}

		try {
			final byte[] btInput = s.getBytes();
			final MessageDigest mdInst = MessageDigest.getInstance("MD5");
			mdInst.update(btInput);
			final byte[] md = mdInst.digest();
			final int j = md.length;
			final char[] str = new char[j * 2];
			int k = 0;
			for (final byte byte0 : md) {
				str[k++] = hexDigits[byte0 >>> 4 & 0xF];
				str[k++] = hexDigits[byte0 & 0xF];
			}
			return new String(str);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 根据年度 获取calendar
	 *
	 * @param year
	 * @return
	 */
	private static Calendar getCalendarFormYear(Integer year) {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		cal.setFirstDayOfWeek(Calendar.MONDAY);
		cal.set(Calendar.YEAR, year);
		return cal;
	}

	/**
	 * 根据 年度 周次 获取 周开始时间
	 *
	 * @param year
	 * @param week
	 * @return
	 */
	public static Date getStartDateOfWeek(Integer year, Integer week) {
		Calendar cal = getCalendarFormYear(year);
		cal.set(Calendar.WEEK_OF_YEAR, week);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTime();

	}

	/**
	 * 根据 年度 周次 获取 周结束时间
	 *
	 * @param year
	 * @param week
	 * @return
	 */
	public static Date getEndDayOfWeek(Integer year, Integer week) {
		Calendar cal = getCalendarFormYear(year);
		cal.set(Calendar.WEEK_OF_YEAR, week);
		cal.add(Calendar.DAY_OF_WEEK, 6);
		cal.set(Calendar.HOUR_OF_DAY, 23);
		cal.set(Calendar.MINUTE, 59);
		cal.set(Calendar.SECOND, 59);
		cal.set(Calendar.MILLISECOND, 999);
		return cal.getTime();
	}
	public static long dateSub(Date beginDate, Date endDate){
		long day = (endDate.getTime()-beginDate.getTime())/(24*60*60*1000);
		return day;
	}

	/*
	根据月份获取当前季度的第一个月份
	 */
	public static Integer getMonthOfQuarterByMonth(Integer month) {
		if(month>=0&&month<=2){
			return 0;
		}
		if(month>=3&&month<=5){
			return 3;
		}
		if(month>=6&&month<=8){
			return 6;
		}
		if(month>=9&&month<=11){
			return 9;
		}
		return 0;
	}

}
